import { _decorator, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Barrier')
export class Barrier extends Component {

    protected onEnable(): void {
        let boxCollider2D = this.node.getComponent(BoxCollider2D);
        boxCollider2D.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        boxCollider2D.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    }

    protected onDisable(): void {
        let boxCollider2D = this.node.getComponent(BoxCollider2D);
        boxCollider2D.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        boxCollider2D.off(Contact2DType.END_CONTACT, this.onEndContact, this);
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        console.log('onBeginContact');
        console.log('碰撞对象：' + otherCollider.name)
    }
    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
        console.log('onEndContact');
        console.log('碰撞对象：' + otherCollider.name)
    }

    start() {

    }

    update(deltaTime: number) {

    }
}


